﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SuperKit {
    /// <summary>
    /// ShellWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ShellWindow : Window, IExecutorOwner {
        public string Args { get; set; }
        public ConfigReader Cfg => ConfigReader.Instance;

        public ShellWindow() {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            Flush($"\n——参数({ArgReader.InitialArgs.Args.Length})——");
            Flush(Args);
            ArgReader.InitialArgs.Execute(this);
        }

        public void Error(string str) {
            Flush(str);
        }

        public void Flush(string str) {
            Dispatcher.Invoke(() => {
                txt_Console.AppendText(str + "\n");
            });
            CogUdpClient.SendLocal(string.Format("{0}|{1}", ArgReader.InitialArgs.ShellIndex, str));
        }

        public string GetArgText() {
            return Args;
        }

        public int GetMaxShellCount() {
            return Math.Max(1, Cfg.MaxShellCount);
        }

        public bool IsUseShell() {
            return ArgReader.InitialArgs.IsShell;
        }

        public void Log(int index, string str) {
            Flush(str);
        }

        public void RefreshLog() {
            
        }

        public void StartUdpServer() {
            
        }

        public void Status(string str) {
            Dispatcher.Invoke(() => {
                label_Status.Content = str;
            });
            CogUdpClient.SendLocal(string.Format("{0}|{1}", ArgReader.InitialArgs.ShellIndex, str));
        }

        void IExecutorOwner.Title(string str) {
            Dispatcher.Invoke(() => {
                Title = str;
            });
        }

        public void CloseForm() {
            Dispatcher.Invoke(() => {
                this.Close();
            });
        }
    }
}
